package com.kingyun.gpsinspection.purificationservice.services.activitiservice.ext.parser;

import org.activiti.bpmn.constants.BpmnXMLConstants;
import org.activiti.bpmn.model.ActivitiListener;
import org.activiti.bpmn.model.ImplementationType;
import org.activiti.bpmn.model.UserTask;
import org.activiti.engine.delegate.TaskListener;
import org.activiti.engine.impl.bpmn.parser.BpmnParse;
import org.activiti.engine.impl.bpmn.parser.handler.UserTaskParseHandler;
import org.activiti.engine.impl.persistence.entity.ProcessDefinitionEntity;
import org.activiti.engine.impl.pvm.process.ActivityImpl;
import org.activiti.engine.impl.task.TaskDefinition;

/**
 * 自定义用户解析类
 */
public class CustomerUserTaskParseHandler extends UserTaskParseHandler {

    @Override
    protected void executeParse(BpmnParse bpmnParse, UserTask userTask) {


        ActivityImpl activity = createActivityOnCurrentScope(bpmnParse, userTask, BpmnXMLConstants.ELEMENT_TASK_USER);

        activity.setAsync(userTask.isAsynchronous());
        activity.setExclusive(!userTask.isNotExclusive());

        TaskDefinition taskDefinition = parseTaskDefinition(bpmnParse, userTask, userTask.getId(), (ProcessDefinitionEntity) bpmnParse.getCurrentScope().getProcessDefinition());
        activity.setProperty(PROPERTY_TASK_DEFINITION, taskDefinition);
        activity.setActivityBehavior(bpmnParse.getActivityBehaviorFactory().createUserTaskActivityBehavior(userTask, taskDefinition));

        ActivitiListener activitiListener = new ActivitiListener();

        activitiListener.setEvent(TaskListener.EVENTNAME_ASSIGNMENT);
        activitiListener.setImplementationType(ImplementationType.IMPLEMENTATION_TYPE_DELEGATEEXPRESSION);
        activitiListener.setImplementation("#{autoCompleteTaskListener}");

        taskDefinition.addTaskListener(TaskListener.EVENTNAME_CREATE,bpmnParse.getListenerFactory().createDelegateExpressionTaskListener(activitiListener));

    }
}
